﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using bts_webapp.Models;
using System.Linq;

namespace bts_webapp
{
    public partial class UserManagement : BasePage
    {
        public void Page_Init(object sender, EventArgs e) { 
            
        }

        public void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            
        }

        public void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var user = e.Item.DataItem as VIBA_User;

            //insert new user
            if (e.Item is GridEditFormInsertItem)
            {
                UxUser uxUser = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxUser;
                //uxUser.BindData(user);
                uxUser.SetFieldCode(true);
                uxUser.InsertOrUpdate = true;
            }
            //update user
            else if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
            {
                UxUser uxUser = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxUser;
                uxUser.SetFieldCode(false);
                uxUser.BindData(user);
                uxUser.InsertOrUpdate = false;
            }

            if (e.Item is GridDataItem) {
                var dataItem = e.Item as GridDataItem;
                dataItem["Role"].Text = GetRoleName(dataItem["Role"].Text, dataItem["Status"].Text, dataItem["UserId"].Text);
                dataItem["Status"].Text = GetStatus(dataItem["Status"].Text);

            }
        }

        private string GetStatus(string status) {
            if (status == "1")
                return "<font color='blue'>Hoạt động</font>";
            return "<font color='red'>Không hoạt động</font>";
        }                                                                                                 

        private string GetRoleName(string role, string status, string userId) {
            string strRole = string.Empty;
            if (role == "0")
                strRole = "Admin";
            if (role == "1")
                strRole = "Nhân viên tổ ViBa";
            if (role == "2")
                strRole = "Nhân viên các đài/trạm";
            if (status == "1")
                return string.Format("<a href='#' onclick=\"return openWin('UserPermission.aspx?uid={1}');\">{0}</a>", strRole, userId);
            return strRole;
        }

        public void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem)
            {
                UxUser uxUser = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxUser;
                var user = uxUser.GetData();
                //check user exists
                if (this.DbContext.VIBA_Users.Any(x => x.User_Id == user.User_Id))
                {
                    uxUser.SetStatus("User đã tồn tại, xin chọn Id khác");
                    e.Canceled = true;
                    return;
                }
                this.DbContext.VIBA_Users.InsertOnSubmit(user);
                this.DbContext.SubmitChanges();
                
            }
        }

        public void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                var userId = (e.Item as GridDataItem)["UserId"].Text;
                var user = this.DbContext.VIBA_Users.FirstOrDefault(u => u.User_Id.ToLower() == userId.ToLower());
                if (user != null)
                {
                    this.DbContext.VIBA_Users.DeleteOnSubmit(user);
                    this.DbContext.SubmitChanges();
                }
            }
        }

        public void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormItem) {
                UxUser uxUser = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxUser;
                var userUpdate = uxUser.GetData();
                var user = this.DbContext.VIBA_Users.FirstOrDefault(u => u.User_Id.ToLower() == userUpdate.User_Id.ToLower());
                if (user != null)
                {
                    user.User_Id = userUpdate.User_Id;
                    user.User_Pass = userUpdate.User_Pass;
                    user.User_Full_Name = userUpdate.User_Full_Name;
                    user.User_Address = userUpdate.User_Address;
                    user.User_Email = userUpdate.User_Email;
                    user.User_Phone = userUpdate.User_Phone;
                    user.User_Role = userUpdate.User_Role;
                    user.User_Status = userUpdate.User_Status;
                    this.DbContext.SubmitChanges();
                }
                                    
            }
        }

        public void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            //get data source
            this.RadGrid1.DataSource = DbContext.VIBA_Users.ToList();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {

        }

        protected void btRefreshGrid_Click(object sender, EventArgs e)
        {
            this.RadGrid1.Rebind();
        }
    }
}